<?php
/*************************************************************************************
   Copyright notice

   This script uses some code parts of the glossary module for phpwcms (c) Oliver Georgi
   (c) 2002-2009 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.

   This script uses some code parts from bmcatalog module
   (c) 2010 Benjamin Matzmorr, www.etype.de

   (c) 2012 webrealisierung gmbh, www.webrealisierung.ch
   Catalogue Module V 1.0.0  -  Date: 2012-06-01

   The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
   A copy is found in the textfile GPL.txt and important notices to the license
   from the author is found in LICENSE.txt distributed with these scripts.

   This script is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
   PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   This copyright notice MUST APPEAR in all copies of the script!
*************************************************************************************/
// ----------------------------------------------------------------
// obligate check for phpwcms constants
if (!defined('PHPWCMS_ROOT')) {
   die("You Cannot Access This Script Directly, Have a Nice Day.");
}
// ----------------------------------------------------------------

//rss
                
$i = 0;
$plugin['ctg_be']['xml'] = array();

  if ($handle = opendir(PHPWCMS_ROOT.'/'.CONTENT_PATH.'rss')) {
    while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
            if (substr($file, -3)=="xml" && substr($file, 0, 13)=="feed-catalog-") {
            $plugin['ctg_be']['xml'][$i] = $file;
            }
          }
        $i++;
        }
      closedir($handle);
  }

  if (count($plugin['ctg_be']['xml'])) {

    sort($plugin['ctg_be']['xml']);
    $plugin['ctg_be']['xml_content'] = array();
    $j=0;

    foreach($plugin['ctg_be']['xml'] as $row) {
      $xml = "";
    $f = fopen( PHPWCMS_ROOT.'/'.CONTENT_PATH.'rss/'.$row, 'r' );
      while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
      fclose( $f );
     preg_match( "/\<lastBuildDate\>(.*?)\<\/lastBuildDate\>/s", $xml, $nameblocks );
        $plugin['ctg_be']['xml_content'][$j]['filename']= $row;
    $plugin['ctg_be']['xml_content'][$j]['descr']= $nameblocks[1];

    $j++;
    }
    $plugin['ctg_be']['xml'] = $plugin['ctg_be']['xml_content'];
  }

//get the structure, article and cp with all nessesary data according visibality etc. and the settings in the contentparts 'plugin:bmcatalog'
$ctg_cp = new ModuleCatalogue();
$ctg_cp->ctg_get_ctlgs();
$ctg_cp->ctg_get_articles();

//version check
if ( defined('PHPWCMS_VERSION') && PHPWCMS_VERSION >= '1.4.5' ) {
  $plugin['ctg_be']['version'] = PHPWCMS_VERSION;
  $plugin['ctg_be']['version_ok'] = 'ok</td><td>';
} elseif ( isset($phpwcms['release']) && $phpwcms['release'] >= '1.4.5' ) {
  $plugin['ctg_be']['version'] = $phpwcms['release'];
  $plugin['ctg_be']['version_ok'] = 'ok</td><td>';
} else {
  $plugin['ctg_be']['version'] = '< 1.4.5';
  $plugin['ctg_be']['version_ok'] = '<span style="color:#F00;">!</span></td><td class="chatlist">module not compatible with this PHPWCMS version!';
}

?>
<!-- settings -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-bottom:1px solid #92A1AF;">
  <tr>
    <td colspan="4" style="height:30px;" class="chatlist">Settings</td>
  </tr>
  <tr>
    <td width="42%" class="chatlist">Frontend Sessions</td>
    <td class="chatlist"><?php
if ( isset($phpwcms['SESSION_FEinit']) && $phpwcms['SESSION_FEinit']== 1 ) {
  echo 'on</td><td>';
} else {
  echo '<span style="color:#F00;">off</span></td><td class="chatlist">&nbsp;required! Change in: conf.inc.php';
}
     ?></td>
  </tr>
  <tr><td colspan="3" style="border-bottom:1px dotted #92A1AF;"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
  <tr>
    <td class="chatlist">mod_rewrite</td>
    <td class="chatlist"><?php
if ( isset($phpwcms['rewrite_url']) && $phpwcms['rewrite_url']== 1 ) {
  echo 'on';
} else {
  echo 'off';
}
     ?></td>
    <td class="chatlist">&nbsp;</td>
  </tr>
  <tr><td colspan="3" style="border-bottom:1px dotted #92A1AF;"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
  <tr>
    <td class="chatlist">PHPWCMS Version is: <?php echo $plugin['ctg_be']['version']; ?></td>
    <td class="chatlist"><?php echo $plugin['ctg_be']['version_ok']; ?></td>
  </tr>
  <tr><td colspan="3" style="border-bottom:1px dotted #92A1AF;"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
  <tr><td colspan="3"><img src="img/leer.gif" alt="" width="1" height="10"></td></tr>
</table>
<!-- end settings -->
<!-- overwiev cp's -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-bottom:1px solid #92A1AF;">
  <tr>
    <td colspan="4" style="height:30px;" class="chatlist"><?php echo $BLM['ctg_cp_title'] ?></td>
  </tr>
<?php
  $plugin['ctg_be']['row_count'] = 0;
  foreach($ctg_cp->plugin_cplist as $row) {
?>
  <tr>
    <td width="38%" valign="top" style="border-top:1px solid #92A1AF;padding-top:5px;">

      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/page_<?php if (!$row["artdata"]["acat_hidden"]) {echo '1';} else {echo '7';} ; ?><?php if ($row["artdata"]["acat_regonly"]) {echo '_locked';} ; ?>.gif" border="0" alt="" /></td>
          <td width="15%"><?php echo $row["artdata"]["acat_id"] ?></td>
          <td width="70%"><?php echo substr($row["artdata"]["acat_name"],0 ,20 ) ?><?php if (strlen($row["artdata"]["acat_name"]) > 20 ) echo '&hellip;'; ?></td>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/visible_11x11a_<?php echo  $row["artdata"]["acat_aktiv"] ?>.gif" border="0" alt="active" /></td>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/public_11x11a_<?php echo  $row["artdata"]["acat_public"] ?>.gif" border="0" alt="public" /></td>
        </tr>
	<tr><td colspan="5" class="rowspacer0x7"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
        <tr>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/text_1.gif" border="0" alt="" /></td>
          <td width="15%"><?php echo $row["artdata"]["article_id"] ?></td>
          <td width="70%"><?php if ($_SESSION['wcs_user_id'] == $row["artdata"]["article_uid"] || $_SESSION["wcs_user_admin"] == 1) { ?>
          <a href="phpwcms.php?do=articles&amp;p=2&amp;s=1&amp;id=<?php echo $row["artdata"]["article_id"] ?>">
<?php }  ?><?php echo  substr($row["artdata"]["article_title"],0 ,20 ) ?><?php if (strlen($row["artdata"]["article_title"]) > 20 ) echo '&hellip;'; ?>
<?php
if ($_SESSION['wcs_user_id'] == $row["artdata"]["article_uid"] || $_SESSION["wcs_user_admin"] == 1) {
echo '</a>';
}
?></td>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/visible_11x11a_<?php echo  $row["artdata"]["article_aktiv"] ?>.gif" border="0" alt="active" /></td>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/public_11x11a_<?php echo  $row["artdata"]["article_public"] ?>.gif" border="0" alt="public" /></td>
        </tr>
	<tr><td colspan="5" class="rowspacer0x7"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
        <tr>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/content_9x11.gif" border="0" alt="" /></td>
          <td width="15%"><?php echo $row["acontent_id"] ?></td>
          <td width="70%"><?php if ($_SESSION['wcs_user_id'] == $row["artdata"]["article_uid"] || $_SESSION["wcs_user_admin"] == 1) { ?>
          <a href="phpwcms.php?do=articles&amp;p=2&amp;s=1&amp;aktion=2&amp;id=<?php echo $row["artdata"]["article_id"] ?>&amp;acid=<?php echo $row["acontent_id"] ?>">
<?php }  ?><?php echo  substr($row["acontent_title"],0 ,20 ) ?><?php if (strlen($row["acontent_title"]) > 20 ) echo '&hellip;'; ?><?php if (strlen($row["acontent_title"]) == 0 ) echo 'no title'; ?>
<?php
if ($_SESSION['wcs_user_id'] == $row["artdata"]["article_uid"] || $_SESSION["wcs_user_admin"] == 1) {
echo '</a>';
}
?></td>
          <td width="5%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/visible_11x11a_<?php echo  $row["acontent_visible"] ?>.gif" border="0" alt="active" /></td>
          <td width="5%">&nbsp;</td>
        </tr>
        <tr>
          <td width="5%">&nbsp;</td>
          <td width="15%">&nbsp;</td>
          <td width="70%"><?php echo  $row["acontent_tstamp"] ?></td>
          <td width="5%"></td>
          <td width="5%">&nbsp;</td>
        </tr>
	<tr><td colspan="5" class="rowspacer0x7"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
      </table>
    </td>
    <td width="2%" style="border-top:1px solid #92A1AF;">&nbsp;</td>
    <td width="2%" style="border-top:1px solid #92A1AF;">&nbsp;</td>
    <td width="58%" valign="top" style="border-top:1px solid #92A1AF;padding-top:5px;">

      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td colspan="3" class="chatlist" style="border-bottom:1px dotted #92A1AF;padding-top:2px;"><?php echo $BLM['ctg_cp_actcals'] ?></td>
        </tr>
<?php
        $i=0;
if ($row['acontent_form']['ctg_catalogs'] == '') $row['acontent_form']['ctg_catalogs'] = array();  //compatibilty to V1.0
if (!isset($row['acontent_form']['ctg_rss_active'])) $row['acontent_form']['ctg_rss_active'] = 0;  //compatibilty to V1.0
        if (count($row['acontent_form']['ctg_catalogs'])) {
          foreach($row['acontent_form']['ctg_catalogs'] as $rowcals) {
            $i++;
?>
        <tr>
          <td width="2%"><?php echo $ctg_cp->plugin_catalogs[$rowcals]['ctgc_id'] ?></td>
          <td width="24%"><?php echo $ctg_cp->plugin_catalogs[$rowcals]['ctgc_name'] ?></td>
          <td width="2%"><img src="<?php echo $phpwcms['modules'][$module]['dir'] ?>img/visible_11x11a_<?php echo $ctg_cp->plugin_catalogs[$rowcals]['ctgc_status'] ?>.gif" border="0" alt="active" /></td>
        </tr>
	      <tr><td colspan="3" style="border-bottom:1px dotted #92A1AF;"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
<?php
          }
        } else {
?>
        <tr>
          <td colspan="3" style="color:#F00;"><?php echo $BLM['ctg_cp_nosel'] ?></td>
        </tr>
	      <tr><td colspan="3" style="border-bottom:1px dotted #92A1AF;"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
<?php
        }
?>
        <tr>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="3" class="chatlist" style="padding-top:2px;"><?php echo $BLM['ctg_cp_actfilters'] ?></td>
        </tr>
        <tr>
          <td colspan="3">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="25%" style="border:1px dotted #92A1AF;">#1&nbsp;-&nbsp;<?php echo $row['acontent_form']['ctg_fltr1_render'] ?></td>
                <td width="25%" style="border:1px dotted #92A1AF;">#2&nbsp;-&nbsp;sel</td>
                <td width="25%" style="border:1px dotted #92A1AF;">#3&nbsp;-&nbsp;sel</td>
                <td width="25%" style="border:1px dotted #92A1AF;">#4&nbsp;-&nbsp;sel</td>
              </tr>
<?php

//get strings for autocompleter js
$cat_sql = "SELECT ctge_tags, ctge_catalogid FROM " . DB_PREPEND . "phpwcms_mod_catalogue_entry WHERE ctge_status!=9 group by ctge_tags ";
$data = _dbQuery($cat_sql);
$tmp_tags_all = array();
$tmp_cats_all = array();
if (count($data)>0) {
  foreach ($data as $val) {

  $tmp_cats = explode("|", trim($val["ctge_catalogid"], "|") );
    foreach($tmp_cats as $v) {
      if (in_array($v, $row['acontent_form']['ctg_catalogs'])) {
        $asd[]=$tmp_cats;
  $tmp_tags = explode("|", trim($val["ctge_tags"], "|") );
  $tmp_tags_all = array_merge($tmp_tags_all, array_map('trim', $tmp_tags));
      }
    }

  }
  $tmp_tags_all = array_unique($tmp_tags_all);
}
$tmp_compare_all = array();
?>
              <tr>
                <td width="25%" style="border:1px dotted #92A1AF;" valign="top"><?php
                if ($row['acontent_form']['ctg_filter1']) echo $row['acontent_form']['ctg_fltr1_dflt'].'<br />';
                $plugin['ctg_be']['fltr'] = explode(",", trim($row['acontent_form']['ctg_filter1']) );
                $plugin['ctg_be']['fltr'] = array_unique( array_filter( array_map('trim', $plugin['ctg_be']['fltr']) ) );
                $tmp_compare_all = array_merge($tmp_compare_all, $plugin['ctg_be']['fltr']);
                $plugin['ctg_be']['fltr'] = array_map('ctg_strlen', $plugin['ctg_be']['fltr']);
                echo implode('<br />', $plugin['ctg_be']['fltr']);
             ?></td>
                <td width="25%" style="border:1px dotted #92A1AF;" valign="top"><?php
                if ($row['acontent_form']['ctg_filter2']) echo $row['acontent_form']['ctg_fltr2_dflt'].'<br />';
                $plugin['ctg_be']['fltr'] = explode(",", trim($row['acontent_form']['ctg_filter2']) );
                $plugin['ctg_be']['fltr'] = array_unique( array_filter( array_map('trim', $plugin['ctg_be']['fltr']) ) );
                $tmp_compare_all = array_merge($tmp_compare_all, $plugin['ctg_be']['fltr']);
                $plugin['ctg_be']['fltr'] = array_map('ctg_strlen', $plugin['ctg_be']['fltr']);
                echo implode('<br />', $plugin['ctg_be']['fltr']);
             ?></td>
                <td width="25%" style="border:1px dotted #92A1AF;" valign="top"><?php
                if ($row['acontent_form']['ctg_filter3']) echo $row['acontent_form']['ctg_fltr3_dflt'].'<br />';
                $plugin['ctg_be']['fltr'] = explode(",", trim($row['acontent_form']['ctg_filter3']) );
                $plugin['ctg_be']['fltr'] = array_unique( array_filter( array_map('trim', $plugin['ctg_be']['fltr']) ) );
                $tmp_compare_all = array_merge($tmp_compare_all, $plugin['ctg_be']['fltr']);
                $plugin['ctg_be']['fltr'] = array_map('ctg_strlen', $plugin['ctg_be']['fltr']);
                echo implode('<br />', $plugin['ctg_be']['fltr']);
             ?></td>
                <td width="25%" style="border:1px dotted #92A1AF;" valign="top"><?php
                if ($row['acontent_form']['ctg_filter4']) echo $row['acontent_form']['ctg_fltr4_dflt'].'<br />';
                $plugin['ctg_be']['fltr'] = explode(",", trim($row['acontent_form']['ctg_filter4']) );
                $plugin['ctg_be']['fltr'] = array_unique( array_filter( array_map('trim', $plugin['ctg_be']['fltr']) ) );
                $tmp_compare_all = array_merge($tmp_compare_all, $plugin['ctg_be']['fltr']);
                $plugin['ctg_be']['fltr'] = array_map('ctg_strlen', $plugin['ctg_be']['fltr']);
                echo implode('<br />', $plugin['ctg_be']['fltr']);
             ?></td>
              </tr><?php

$result = array_diff($tmp_tags_all, $tmp_compare_all );
if (count($result)) {

  echo '
    <tr>
      <td colspan="4" width="100%" style="border:1px dotted #92A1AF;">'.$BLM['ctg_cp_notassigned'].'&nbsp;
      <img src="'.$phpwcms['modules'][$module]['dir'].'img/visible_11x11a_9.gif" border="0" alt="" /></td>
    </tr>
    <tr>
      <td colspan="4" width="100%" style="border:1px dotted #92A1AF;">'.implode('<br />', $result).'</td>
    </tr>'.LF;
}

            ?></table>
          </td>
        </tr>
        <tr>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="3">RT: {SHOW_CONTENT:CP,<?php echo $row["acontent_id"]; ?>}</td>
        </tr>
        <tr>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="3" class="chatlist" style="padding-top:2px;"><?php echo $BLM['ctg_rss_feed'] ?></td>
        </tr>

        <tr>
          <td colspan="3"><?php

if (count($plugin['ctg_be']['xml'])) {
          echo '<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="">';
          $plugin['ctg_be']['rows_xml'] = 1;
          foreach($plugin['ctg_be']['xml'] as $rowrss) {
             preg_match( "/.*?-(\d+?)-(\d+?)\.xml/s", $rowrss["filename"], $ids );

            if($ids[1] == $row["artdata"]["article_id"] && $ids[2] == $row["acontent_id"]) {
            echo LF.'<tr bgcolor="#F3F5F8">'.LF.'<td width="10%" valign="top" style="padding-top:5px;">';
            echo '<a href="'.CONTENT_PATH.'rss/'.html_specialchars($rowrss["filename"]).'" target="_blank"><img src="'.$phpwcms['modules'][$module]['dir'].'img/feed.gif" border="0" alt="'.$BLM['ctg_cp_rss'].'" title="'.$BLM['ctg_cp_rss'].'" /></a></td>'.LF;
            echo '<td class="dir" width="75%">'.html_specialchars($rowrss["filename"]).'<br />'."\n";
            echo date("Y-m-d H:i:s", strtotime($rowrss["descr"])).'</td>'."\n";
            echo '<td class="dir" width="5%" height="20"><a href="'.ctg_map_url('controller=module').'&amp;update=rss&amp;art_id='.$ids[1].'&amp;cp_id='.$ids[2].'" title="'.$BLM['ctg_cp_update'].'"><img src="'.$phpwcms['modules'][$module]['dir'].'img/feed_disk.gif" border="0" alt="'.$BLM['ctg_cp_update'].'" /></a> </td>'."\n";
            echo '<td width="5%" align="right" nowrap="nowrap"><img src="'.$phpwcms['modules'][$module]['dir'].'img/active_11x11a_'.$row['acontent_form']['ctg_rss_active'].'.gif" border="0" alt="active" /></td>'."\n";
            echo '<td width="5%" align="right" nowrap="nowrap" class="button_td">';
if ($_SESSION['wcs_user_id'] == $row["artdata"]["article_uid"] || $_SESSION["wcs_user_admin"] == 1) {
            echo '<a href="'.ctg_map_url('controller=module').'&amp;delete=rss&amp;art_id='.$ids[1].'&amp;cp_id='.$ids[2].'"';
            echo ' title="delete: '.html_specialchars($rowrss["filename"]).'"';
            echo ' onclick="return confirm(\''.$BLM['ctg_delete_entry'].' '.js_singlequote($rowrss["filename"]).'\');">';
            echo '<img src="'.$phpwcms['modules'][$module]['dir'].'img/trash_13x13_1.gif" border="0" alt=""></a>';
}
            echo "</td></tr>";
            }

          } //end foreach

          echo '</table>'.LF;
}

?>
          </td>
        </tr>
	      <tr><td colspan="3" style="height:20px;"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
      </table>
    </td>
  </tr>
<?php
    $plugin['ctg_be']['row_count']++;
  } ///end foreach
?>
</table>
<!-- end overwiev cp's -->
<?php
  if($plugin['ctg_be']['row_count'] == 0){
// nothing found
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr><td><img src="img/leer.gif" alt="" width="1" height="3"></td></tr>
	<tr><td bgcolor="#92A1AF"><img src="img/leer.gif" alt="" width="1" height="1"></td></tr>
	<tr><td><?php echo $BLM['no_entry'] ?></td></tr>
</table>
<?php
  }

?>